package com.randev.snap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.randev.snapcamera.CameraActivity;
import com.randev.snapgallery.GalleryActivity;

public class MainMenuActivity extends Activity implements OnClickListener{

	ImageButton iBtnGallery, iBtnTakePictures;
	private final String TAG = "MainMenuActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);
		
		iBtnGallery = (ImageButton) findViewById(R.id.bGallery);
		iBtnTakePictures = (ImageButton) findViewById(R.id.bTakePicture);
		iBtnGallery.setOnClickListener(this);
		iBtnTakePictures.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.bGallery:
			try {
				Intent galleryActivityIntent = new Intent(MainMenuActivity.this, GalleryActivity.class);
				finish();
				startActivity(galleryActivityIntent);
			} catch (ActivityNotFoundException e) {
				Log.w(TAG,"Unable to start GalleryActivity" +  e.toString());
			} 
			break;
		
		case R.id.bTakePicture:
        	try {
        		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    			Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
    			 
    			String imageCount = prefs.getString("ImageCount", null);
    			editor.putString("ImagesAvailable", imageCount);
    			editor.commit();
    			String folderId = prefs.getString("FolderId", null);
    			int fId = Integer.parseInt(folderId);
        		editor.putString("FolderId", String.valueOf(fId+1));
    			editor.commit();
    			
				Intent cameraActivityIntent = new Intent(MainMenuActivity.this, CameraActivity.class);
				finish();
				startActivity(cameraActivityIntent);
			} catch (ActivityNotFoundException e) {
				Log.w(TAG,"Unable to start cameraActivity" +  e.toString());
			} catch (NullPointerException e) {
				Log.w(TAG, "Unable to set preferences to CameraActivity: NullPointerException " + e.toString());
			} catch (ClassCastException e) {
				Log.w(TAG, "Unable to set preferences to CameraActivity: ClassCastException " + e.toString());
			}
			break;
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	    	AlertDialog.Builder alertExit = new AlertDialog.Builder(MainMenuActivity.this);
	    	alertExit.setTitle(MainMenuActivity.this.getString(R.string.warning));
	    	TextView tViewAlertExit = new TextView(MainMenuActivity.this);
	    	tViewAlertExit.setHeight(90);
	    	tViewAlertExit.setGravity(Gravity.CENTER_HORIZONTAL);
	    	alertExit.setView(tViewAlertExit);
	    	tViewAlertExit.setText(MainMenuActivity.this.getString(R.string.info_exit_prompt));
	    	alertExit.setPositiveButton(MainMenuActivity.this.getString(R.string.yes), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
			        String folderId =  prefs.getString("FolderId", null);
		            Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
					int fId = Integer.parseInt(folderId);
		    		editor.putString("FolderId", String.valueOf(fId+1));
					editor.commit(); 
					finish();
				}
			});
	    	alertExit.setNegativeButton(MainMenuActivity.this.getString(R.string.no), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
	    	alertExit.show();
	    	return true;
	    }

	    return super.onKeyDown(keyCode, event);
	}

}
